package com.cch.cooperation.api.zone.controller;

import com.cch.cooperation.api.auth.dto.LoginUserDTO;
import com.cch.cooperation.api.common.request.IdReq;
import com.cch.cooperation.api.zone.dto.ZonePostAddDTO;
import com.cch.cooperation.api.zone.dto.ZonePostDTO;
import com.cch.cooperation.api.zone.dto.ZonePostQueryDTO;
import com.cch.cooperation.api.zone.service.ZonePostServiceImpl;
import com.cch.cooperation.common.page.PageList;
import com.cch.cooperation.common.web.response.Response;
import com.cch.cooperation.common.web.session.Session;
import com.cch.cooperation.common.web.session.SessionManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 空间动态接口
 *
 * @author cch
 */
@Api(tags = "空间动态接口")
@RestController
@RequestMapping("/api/zone/post")
public class ZonePostController {

    @Resource
    private ZonePostServiceImpl zonePostService;
    @Resource
    private SessionManager sessionManager;

    @ApiOperation("查询分页列表")
    @GetMapping("/queryPageList")
    public Response<PageList<ZonePostDTO>> queryPageList(ZonePostQueryDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        PageList<ZonePostDTO> data = zonePostService.queryPageList(req, session.getData().getId());
        return Response.success(data);
    }

    @ApiOperation("根据id获取动态详情")
    @GetMapping("/getById")
    public Response<ZonePostDTO> getById(@ApiParam(value = "id", required = true)String id) {
        ZonePostDTO data = zonePostService.getById(id);
        return Response.success(data);
    }

    @ApiOperation("新增动态")
    @PostMapping("/add")
    public Response<Void> add(@RequestBody ZonePostAddDTO req) {
        Session<LoginUserDTO> session = sessionManager.get();
        req.setUserId(session.getData().getId());
        zonePostService.add(req);
        return Response.success();
    }

    @ApiOperation("根据id删除")
    @PostMapping("/deleteById")
    public Response<Void> deleteById(@RequestBody IdReq req) {
        zonePostService.deleteById(req.getId());
        return Response.success();
    }

}
